//
//  MyViewController.m
//  Item A House
//
//  Created by dllo on 15/5/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MyViewController.h"

#import "MyCollectionViewController.h"
#import "RentalCollViewController.h"
#import "NewCollViewController.h"
#import "NewCollectionModel.h"
#import "OldCollectionModel.h"
#import "RentalCollectionModel.h"

@interface MyViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation MyViewController
- (void)dealloc
{
    [_tableArray release];
    [_button release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        NSString *str1 = @"新房收藏";
        NSString *str2 = @"二手房收藏";
        NSString *str3 = @"租房收藏";
        [_tableArray addObject:str1];
        [_tableArray addObject:str2];
        [_tableArray addObject:str3];
        self.title = @"我的";
        
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    
    // 重复池
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
    }
    NSString *str = [_tableArray objectAtIndex:indexPath.row];
    [cell.textLabel setText:str];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 4, self.view.frame.size.width, self.view.frame.size.height - 8)];
    UIImage *image = [UIImage imageNamed:@"qiu.jpg"];
    cell.imageView.image = image;
    [imageView setBackgroundColor:[UIColor whiteColor]];
    [cell setBackgroundView:imageView];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 判断用户是否登录,已登录跳转我的收藏页面,没登录则跳转登录界面
//    NSString *str = [self readString];
    if (indexPath.row == 0) {
        NSArray *array = [NewCollectionModel readArray];
        if (array.count != 0) {
            NewCollViewController *new = [[NewCollViewController alloc] init];
            [self.navigationController pushViewController:new animated:YES];
            [new release];
        } else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"未藏通知" message:@"什么都没有!" delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
            [alert show];
        }
    } else if ( indexPath.row == 1){
        NSArray *array = [OldCollectionModel readArray];
        if (array.count != 0) {
            
            MyCollectionViewController *collection = [[MyCollectionViewController alloc] init];
            [self.navigationController pushViewController:collection animated:YES];
            [collection release];
        } else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"未藏通知" message:@"什么都没有!" delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
            [alert show];
        }
    } else {
        NSArray *array = [RentalCollectionModel readArray];
        if (array.count != 0) {
            RentalCollViewController *rental = [[RentalCollViewController alloc] init];
            [self.navigationController pushViewController:rental animated:YES];
            [rental release];
        } else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"未藏通知" message:@"什么都没有!" delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
            [alert show];
            
        }
    }
}

- (void)createTable
{
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, 180)];
    [image setImage:[UIImage imageNamed:@"别墅.jpg"]];
    [self.view addSubview:image];
    
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    // 设置table背景颜色
    [table setBackgroundColor:[UIColor whiteColor]];
    // 设置代理人
    table.dataSource = self;
    table.delegate = self;
    // 高
    table.rowHeight = 60;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    [table release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTable];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 读取路径
- (NSString *)readString
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [array lastObject];
    path = [NSString stringWithFormat:@"%@/%@", path, @"loginState.txt"];
    
    
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    return str;
}

// 存储路径
- (void)writeString
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [array lastObject];
    path = [NSString stringWithFormat:@"%@/%@", path, @"loginState.txt"];
    
    NSError *error = nil;
    NSString *str = @"no";
    [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
}
@end
